﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Kylin { 

    public class DataTable
    {

        private Dictionary<string, TableProperty> dic = new Dictionary<string, TableProperty>();

        public void LoadFile(string path)
        {
            string content = ResourceLoader.LoadTable(path);

            string[] lines = content.Split('\n');

            if (lines.Length < 3)
            {
                Debug.LogError("读取到一个不符合规范的表格文件：" + path);
                return;
            }

            string[] keys = lines[0].Split('\t');
            string[] types = lines[1].Split('\t');


            for (var i = 3; i < lines.Length; ++i)
            {
                var values = lines[i].Split('\t');
                var property = new TableProperty();
                
                dic[values[0]] = property;

                for (var index = 0; index < values.Length; ++index)
                {
                    if (types[index].Equals("int"))
                    {
                        property.SetInt(keys[index], int.Parse(values[index]));
                    }
                    else if (types[index].Equals("string"))
                    {
                        property.SetString(keys[index], values[index]);
                    }
                    else if (types[index].Equals("float"))
                    {
                        property.SetFloat(keys[index], float.Parse(values[index]));
                    }
                }
            }
        }

        public void Unload()
        {
            foreach(KeyValuePair<string, TableProperty> kv in dic)
            {
                dic[kv.Key].Unload();
            }

            dic.Clear();
        }
    }

}
